package com.frkj.order.util;

import com.frkj.order.cache.LoginOperatorCache;
import com.frkj.order.model.UserOrder;
import com.jfinal.plugin.activerecord.Db;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * Create-Date: 2016/7/4
 * Description:
 */
public class UserOrderCleaner {

    private static Logger logger = LoggerFactory.getLogger(UserOrder.class);

    public static void cleanData() {
        logger.warn("execute data cleanData");
        try {
            List<UserOrder> userOrderList = UserOrder.me.find("select * from userorder");
            Object[][] records = new Object[userOrderList.size()][14];
            UserOrder userOrder = null;
            for (int i = 0; i < records.length; i++) {
                userOrder = userOrderList.get(i);
                Object[] attrs = new Object[14];
                attrs[0] = userOrder.get("number");
                attrs[1] = userOrder.get("user_type");
                attrs[2] = userOrder.get("open_id");
                attrs[3] = userOrder.get("business_id");
                attrs[4] = userOrder.get("business_name");
                attrs[5] = userOrder.get("business_tag");
                attrs[6] = userOrder.get("operator_id");
                attrs[7] = userOrder.get("operator_name");
                attrs[8] = userOrder.get("status");
                attrs[9] = userOrder.get("create_time");
                attrs[10] = userOrder.get("start_time");
                attrs[11] = userOrder.get("end_time");
                attrs[12] = userOrder.get("cost_time");
                attrs[13] = userOrder.get("window");
                records[i] = attrs;
            }
            String sql = "insert into historyorder(number,user_type,open_id,business_id,business_name,business_tag,operator_id,operator_name,status,create_time,start_time,end_time,cost_time,window) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
            Db.batch(sql, records, 10000);
            logger.warn("...move table data success");
        } catch (Exception ex) {
            logger.error(ex.toString());
        }

        Db.update("delete from userorder");
        logger.warn("...cleanData table success.");

        Db.update("delete from loginoperator");
        LoginOperatorCache.loadData();
        logger.warn("...all operators logout success.");

        logger.warn("data cleanData finished.");
    }
}
